package dbzx.pdf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class FontTest {
	
	/**
	 * 生成pdf文件
	 */
	public void createPdf(Font font) throws FileNotFoundException, DocumentException {
		
		String path = "E:/demo/pdfCreat/"+System.currentTimeMillis()+".pdf";
		File file = new File(path);
		file.getParentFile().mkdirs();
		Document doc = new Document(PageSize.A4);
		PdfWriter.getInstance(doc, new FileOutputStream(file));
		doc.open();
		doc.add(new Paragraph("字体测试",font));
		doc.close();
	}
	
	@Test
	/**
	 * 使用windows系统下的字体,new Font方式
	 */
	public void test1_1() throws DocumentException, IOException {
		String path = "C:/WINDOWS/Fonts/simhei.ttf";//windows里的字体资源路径
		BaseFont bf = BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		Font font = new Font(bf, 10f, Font.NORMAL, BaseColor.BLACK);
		createPdf(font);
	}
	
	@Test
	/**
	 * 使用windows系统下的字体,FontFactory方式
	 */
	public void test1_2() throws DocumentException, IOException {
		String path = "C:/WINDOWS/Fonts/simhei.ttf";//windows里的字体资源路径
		Font font = FontFactory.getFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED,10f, Font.NORMAL, BaseColor.BLACK);
		createPdf(font);
	}
	
	@Test
	/**
	 * 使用自己查找的字体,FontFactory方式
	 */
	public void test2() throws DocumentException, IOException {
		String path = "src/main/resources/file/pdf/font/SIMYOU.TTF";//自己的字体资源路径
		Font font = FontFactory.getFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED,10f, Font.NORMAL, BaseColor.BLACK);
		createPdf(font);
	}
	
	
	/**
	 * 使用iTextAsian.jar中的字体，FontFactory方式
	 */
	@Test
	public void test3() throws DocumentException, IOException {
		Font font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED,10f, Font.NORMAL, BaseColor.BLACK);
		createPdf(font);
	}
	
	
	
	
	
	
}
